package com.zj.androidcamera.thread;

import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.zj.androidcamera.bean.FaceBean;
import com.zj.androidcamera.util.ByteUtil;

import java.util.Objects;

public class FaceThread extends Thread {
    public FaceHandler handler;
    protected FaceInterface faceInterface;

    public void setFaceInterface(FaceInterface faceInterface) {
        this.faceInterface = faceInterface;
    }

    @Override
    public void run() {
//        super.run();
        Looper.prepare();

        handler = new FaceHandler();

        Looper.loop();
    }

    class FaceHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            FaceBean faceBean = (FaceBean) msg.obj;
            byte[] data = faceBean.data;
            int width = faceBean.width;
            int height = faceBean.height;
            Rect rect = faceBean.rect;

            int[] pixels = ByteUtil.convertYUV420spToARGB(data, width,
                    height, rect);

            if (Objects.nonNull(faceInterface)) {
                faceInterface.handleFace(pixels, rect.width(), rect.height());
            }
        }
    }

    public interface FaceInterface {
        void handleFace(int[] pixels, int width, int height);
    }
}
